﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.Corp;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.Constructor.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ConstructorRepository : Repository<ConstructorInfo, DefaultDbContext>
    {
        private readonly ILogger logger;

        public ConstructorRepository(DefaultDbContext dbContext, IApplicationContext applicationContext, ILogger<ConstructorRepository> logger)
            : base(dbContext, applicationContext)
        {
            this.logger = logger;
        }

        public bool CodeExists(string code)
        {
            return DbContext.Exists<ConstructorInfo>("Code=@0", StringUtility.StripSQLInjection(code));
        }

        public IPagedList<ConstructorInfo> GetConstructors(ConstructorQuery query, int pageIndex, int pageSize)
        {
            Sql sql = Sql.Builder;
            sql.Select("constructor_id")
                .From("constructor");
            if (!string.IsNullOrEmpty(query.Keywords))
                sql.Where("(name Like @0 or code Like @0)", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
            sql.OrderBy("create_time DESC");
            var result = GetPagingEntities(pageSize, pageIndex, sql);
            return result;
        }

        public int? GetConstructorId(string constructorCode)
        {
            Sql sql = Sql.Builder.Select("constructor_id").From("constructor").Where("code=@0", constructorCode);
            var id = DbContext.FirstOrDefault<int>(sql);
            if (id == 0)
                return null;
            return id;
        }

        public ConstructorInfo? GetConstructorInfo(string constructorCode)
        {
            var id = GetConstructorId(constructorCode);
            if (!id.HasValue || id == 0)
                return null;
            return Get(id);
        }

        public ConstructorInfo? GetConstructorInfoByName(string name)
        {
            Sql sql = Sql.Builder.Select("constructor_id").From("constructor").Where("name=@0", name);
            var id = DbContext.FirstOrDefault<int>(sql);
            if (id == 0)
                return null;
            return Get(id);
        }

        public IEnumerable<ConstructorInfo> GetConstructorInfosByName(IEnumerable<string> names)
        {
            if (names == null || names.Count() == 0)
                return Enumerable.Empty<ConstructorInfo>();
            Sql sql = Sql.Builder.Select("constructor_id").From("constructor").Where("name in (@0)", names);
            var id = DbContext.Fetch<int>(sql);

            return PopulateEntitiesByEntityIds(id);
        }

        public bool UpdateDefaultProjectId(ConstructorInfo constructorInfo, int projectId)
        {
            if (constructorInfo == null)
                return false;
            var sql_update = Sql.Builder;
            sql_update.Append("Update constructor SET project_id = @0 where constructor_id = @1", projectId, constructorInfo.ConstructorId);//局部更新数据库
            int affectCount = DbContext.Execute(sql_update);
            if (affectCount > 0)
            {
                constructorInfo.ProjectId = projectId;
                base.OnUpdated(constructorInfo);//更新内存数据
                return true;
            }
            return false;
        }
    }
}